/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/util/ExceptionUtils.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: ExceptionUtils.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (Mon, 02 Jul 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.commons.util;

/**
 * @author Elan
 * @date 2010-10-15
 */
public class ExceptionUtils {
	public static String format(String msg, Exception e) {
		return format(msg, e, false);
	}

	public static String format(String msg, Exception e, boolean diagonal) {
		String rtxt = "";

		if (e != null && e.getMessage() != null) {
			rtxt = e.getMessage();
			if (diagonal) {
				rtxt = rtxt.replaceAll("'", "\\\\\\\\'");
			}
		}

		if (msg != null) {
			return msg.replaceAll("\\$\\{message\\}", rtxt);
		}
		return msg;
	}

	public static String getClassMessage(Exception e) {
		if (e != null) {
			return e.getClass().getName() + ":" + e.getMessage();
		}
		return "";
	}

	public static String getMessage(Exception e) {
		if (e != null) {
			return e.getMessage();
		}
		return "";
	}

	/**
	 * @param causedTrace
	 */
	public static String getStackTraceAsString(StackTraceElement[] causedTrace) {
		final StringBuffer buffer = new StringBuffer();
		StackTraceElement traceElement = null;
		for (final StackTraceElement element : causedTrace) {
			traceElement = element;
			buffer.append("java file  :").append(traceElement.getFileName());
			buffer.append("class name :").append(traceElement.getClassName());
			buffer.append("line number:").append(traceElement.getLineNumber());
			buffer.append("method name:").append(traceElement.getMethodName());
			buffer.append("<br>");
		}
		return buffer.toString();
	}
}
